package com.liu.mybatisPlus.climbingTower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.mybatisPlus.climbingTower.entity.base.ClimbingTowerArticleAttributes;
import com.liu.mybatisPlus.climbingTower.entity.base.ClimbingTowerArticleInfo;
import com.liu.mybatisPlus.climbingTower.entity.base.ClimbingTowerUserEquip;
import com.liu.mybatisPlus.climbingTower.mapper.ClimbingTowerUserEquipMapper;
import com.liu.mybatisPlus.climbingTower.service.ClimbingTowerArticleAttributesService;
import com.liu.mybatisPlus.climbingTower.service.ClimbingTowerArticleInfoService;
import com.liu.mybatisPlus.climbingTower.service.ClimbingTowerUserEquipService;
import com.liu.qqRobot.climbingTower.entity.dto.ClimbingTowerUserEquipDTO;
import com.liu.qqRobot.climbingTower.entity.dto.UserEquipAttributeDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuChengLong
 * @since 2025-04-01
 */
@Service
public class ClimbingTowerUserEquipServiceImpl extends ServiceImpl<ClimbingTowerUserEquipMapper, ClimbingTowerUserEquip> implements ClimbingTowerUserEquipService {

    @Resource
    private ClimbingTowerArticleInfoService climbingTowerArticleInfoService;

    @Resource
    private ClimbingTowerArticleAttributesService climbingTowerArticleAttributesService;

    @Override
    public ClimbingTowerUserEquipDTO getUserEquip(Long groupId, Long userId) {
        QueryWrapper<ClimbingTowerUserEquip> climbingTowerUserEquipQueryWrapper = new QueryWrapper<>();
        climbingTowerUserEquipQueryWrapper.eq("group_id", groupId);
        climbingTowerUserEquipQueryWrapper.eq("user_id", userId);
        ClimbingTowerUserEquip climbingTowerUserEquip = super.getOne(climbingTowerUserEquipQueryWrapper);
        if (climbingTowerUserEquip == null) {
            return new ClimbingTowerUserEquipDTO();
        }
        List<Integer> equipArticleIdList = Arrays.asList(climbingTowerUserEquip.getBody(), climbingTowerUserEquip.getHead(),
                climbingTowerUserEquip.getLeftLeg(), climbingTowerUserEquip.getLeftFoot(), climbingTowerUserEquip.getLeftHand(),
                climbingTowerUserEquip.getRightLeg(), climbingTowerUserEquip.getRightFoot(), climbingTowerUserEquip.getRightHand());
        Map<Integer, String> articleNameMap = climbingTowerArticleInfoService.listByIds(equipArticleIdList).stream()
                .collect(Collectors.toMap(ClimbingTowerArticleInfo::getId, ClimbingTowerArticleInfo::getName));
        ClimbingTowerUserEquipDTO climbingTowerUserEquipDTO = new ClimbingTowerUserEquipDTO();
        climbingTowerUserEquipDTO.setBody(articleNameMap.get(climbingTowerUserEquip.getBody()));
        climbingTowerUserEquipDTO.setHead(articleNameMap.get(climbingTowerUserEquip.getHead()));
        climbingTowerUserEquipDTO.setLeftFoot(articleNameMap.get(climbingTowerUserEquip.getLeftFoot()));
        climbingTowerUserEquipDTO.setRightFoot(articleNameMap.get(climbingTowerUserEquip.getRightFoot()));
        climbingTowerUserEquipDTO.setLeftLeg(articleNameMap.get(climbingTowerUserEquip.getLeftLeg()));
        climbingTowerUserEquipDTO.setRightLeg(articleNameMap.get(climbingTowerUserEquip.getRightLeg()));
        climbingTowerUserEquipDTO.setLeftHand(articleNameMap.get(climbingTowerUserEquip.getLeftHand()));
        climbingTowerUserEquipDTO.setRightHand(articleNameMap.get(climbingTowerUserEquip.getRightHand()));
        return climbingTowerUserEquipDTO;
    }

    @Override
    public UserEquipAttributeDTO calcUserEquipAttribute(Long groupId, Long userId) {
        int hp = 0;
        int aggressivity = 0;
        int agile = 0;
        QueryWrapper<ClimbingTowerUserEquip> climbingTowerUserEquipQueryWrapper = new QueryWrapper<>();
        climbingTowerUserEquipQueryWrapper.eq("group_id", groupId);
        climbingTowerUserEquipQueryWrapper.eq("user_id", userId);
        ClimbingTowerUserEquip climbingTowerUserEquip = super.getOne(climbingTowerUserEquipQueryWrapper);
        if (climbingTowerUserEquip != null) {
            List<Integer> equipArticleIdList = Arrays.asList(climbingTowerUserEquip.getBody(), climbingTowerUserEquip.getHead(),
                    climbingTowerUserEquip.getLeftLeg(), climbingTowerUserEquip.getLeftFoot(), climbingTowerUserEquip.getLeftHand(),
                    climbingTowerUserEquip.getRightLeg(), climbingTowerUserEquip.getRightFoot(), climbingTowerUserEquip.getRightHand());
            QueryWrapper<ClimbingTowerArticleAttributes> climbingTowerArticleAttributesQueryWrapper = new QueryWrapper<>();
            climbingTowerArticleAttributesQueryWrapper.in("article_id", equipArticleIdList);
            List<ClimbingTowerArticleAttributes> climbingTowerArticleAttributesList = climbingTowerArticleAttributesService.list(climbingTowerArticleAttributesQueryWrapper);
            Map<Integer, List<ClimbingTowerArticleAttributes>> climbingTowerArticleAttributesMap = climbingTowerArticleAttributesList.stream().collect(Collectors.groupingBy(ClimbingTowerArticleAttributes::getArticleId));
            for (Integer equipArticleId : equipArticleIdList.stream().filter(Objects::nonNull).toList()) {
                List<ClimbingTowerArticleAttributes> climbingTowerArticleAttributes = climbingTowerArticleAttributesMap.get(equipArticleId);
                for (ClimbingTowerArticleAttributes climbingTowerArticleAttribute : climbingTowerArticleAttributes) {
                    int value = Integer.parseInt(climbingTowerArticleAttribute.getValue());
                    switch (climbingTowerArticleAttribute.getKey()) {
                        case "hp" -> {
                            hp += value;
                        }
                        case "aggressivity" -> {
                            aggressivity += value;
                        }
                        case "agile" -> {
                            agile += value;
                        }
                    }
                }
            }
        }
        return new UserEquipAttributeDTO(hp, aggressivity, agile);
    }
}
